package com.wfyn.reggie.modules.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wfyn.reggie.common.web.base.BaseController;
import com.wfyn.reggie.common.web.domain.Result;
import com.wfyn.reggie.modules.sys.domain.Category;
import com.wfyn.reggie.modules.sys.param.PageListRequest;
import com.wfyn.reggie.modules.sys.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 菜品类别
 * @author wfyn
 * @date 2023/9/28
 */
@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 查询分类列表
     * @param pageListRequest pageListRequest
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Category>> categoryPage(PageListRequest pageListRequest){

        return categoryService.categoryPage(pageListRequest);
    }

    /**
     * 新增分类
     * @param category  category
     * @return
     */
    @PostMapping
    public Result<String> addCategory(@RequestBody Category category){
        return Result.auto(categoryService.save(category),"添加成功","添加失败");

    }


    /**
     * 根据id修改分类信息
     * @param category
     * @return
     */
    @PutMapping
    public Result<String> updateCategory(@RequestBody Category category){

        return Result.auto(categoryService.updateById(category),"修改成功","修改失败");
    }

    /**
     *  根据id删除分类信息
     * @param id 菜品分类id
     * @return
     */
    @DeleteMapping
    public Result<String> deleteCategory(Long id){

        return  Result.auto(categoryService.deleteCategory(id),"分类信息删除成功","分类信息删除失败");

    }

    /**
     * 根据条件查询套餐数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public Result selectCategory(Category category)
    {

        List<Category> list = categoryService.lambdaQuery()
                .eq(category.getType() != null,Category::getType, category.getType())
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getUpdateTime)
                .list();
//        //条件构造器
//        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
//        //添加条件
//        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
//        //添加排序条件
//        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
//
//        List<Category> list = categoryService.list(queryWrapper);
//        return R.success(list);

        return Result.success(list);



    }



}
